/**
 * 
 */
package action.admin;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import model.Coursetype;
import model.CoursetypeDAO;
import model.TeacherDAO;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.opensymphony.xwork2.ActionSupport;

import util.*;
import model.*;
/** 
 * @作者 Zouzr 
 * @创建日期 Dec 10, 2008 
 * @版本 V 1.0 
 */

public class StudentAction extends ActionSupport
		{
		private String actiontype="1";//actiontype用于标识是获取所有的记录还是搜索记录,默认是获取所有记录即”1“
		
		private String actionname = "getallstudents";//action名称
		
		private String id;//学生表id
		
		private String stunumber;//学生学号
		
		private String stuname;//学生姓名
		
		private String stusex;//学生性别
		
		private String specialdepartmentId;//专业对应的学院id
		
		private String stuspecialId;//学生专业id
		
		private int currentpage=1;//当前页数,当获得的值为null或""时初始化为1;
		
		private int pageresult=20;//每页显示数据的条数,当获得的值为null或""时初始化为20;
		
		private int pagecount;//总页数
		
		private int resultcount;//总记录
	
	 	private File upload;// 实际上传文件

	    private String uploadContentType; // 文件的内容类型

	    private String uploadFileName; // 上传文件名
	    
	    private List<Object> objects = new ArrayList<Object>();
	    
	    private List<Student> students = new ArrayList<Student>();//存放所有student记录
	    
	    private List<Special> specials = new ArrayList<Special>();//存放student对应的special记录
	    
	    private List<Department> departments = new ArrayList<Department>();//存放Student对应的department记录
	    
	    private List<Special> totalspecials = new ArrayList<Special>();//存放某一学院对应的所有的special信息
	    
	    private List<Department> totaldepartments = new ArrayList<Department>();//存放所有的department信息
	    
	    private ServletActionContext context;
	    
	    //保存上传文件的目录，相对于web应用程序的根路径，在struts.xml文件中配置
	    private String uploadDir;

	    /**
	     * 验证数据合法性
	     */
	    public void validate()
	    {
	    	//this.addFieldError("msg.hello","必须输入hello!");
	    	//this.addActionError("处理动作失败!");
	    	//this.addActionMessage("提交成功");
	    	//获得所有学院	
	    	ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
	        DepartmentDAO departmentdao = (DepartmentDAO)context.getBean("DepartmentDAOProxy");
	        List temp_totaldepartments = departmentdao.findAll();
	        for(int i=0;i<temp_totaldepartments.size();i++)
	        {
	        	Department d = new Department();
	        	d = (Department)temp_totaldepartments.get(i);
	        	totaldepartments.add(d);
	        	d = null;
	        }
	    	this.context.getRequest().setAttribute("totaldepartments", totaldepartments);
	    }
	    
	    /**
	     * 上传专业信息excel文件，并将文件数据导入到数据库中
	     * @return
	     * @throws Exception
	     */
	    public String importStudentInfo() throws Exception {
	        try {
	        	if(uploadFileName==null)
	        	{
	        		this.addActionError("无效的文件路径!");
            		return INPUT;
	        	}
	        	//application/vnd.openxmlformats-officedocument.spreadsheetml.sheet（Excel2007的文件类型）
	        	System.out.println("文件类型"+uploadContentType);
	        	System.out.println("文件上传名"+uploadFileName);
	            String targetDirectory = context.getServletContext().getRealPath(uploadDir);
	            System.out.println("目标路径:"+targetDirectory);
	            String targetFileName = uploadFileName;
	            File target = new File(targetDirectory, targetFileName);
	            FileUtils.copyFile(upload, target);    
	            setUploadFileName(target.getPath());//保存文件的存放路径
		        } 
	        	catch (Exception e) 
	        	{
		        	System.out.println(e.getMessage());
		        }
	        	
	        	//先将数据库中原来的教师的记录都删除，再导入新的所有教师的信息
	        	ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
		        StudentDAO Studentdao = (StudentDAO)context.getBean("StudentDAOProxy");
		        Studentdao.deleteAllStudentInfo();
	        	//将上传的xls中的数据插入到数据库中
	        	String returnstring=ImportStudent.importIntoStudent(getUploadFileName());
	        	if(!returnstring.equals(""))
	        	{
	        		this.addActionError(returnstring);
	        		return INPUT;
	        	}
	        return SUCCESS;

	    }

	    public String getStudentById()
	    {
	    	//System.out.println(context.getRequest().getParameter("currentpage"));
	    	//System.out.println(context.getRequest().getParameter("pageresult"));
	    	ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
	        StudentDAO Studentdao = (StudentDAO)context.getBean("StudentDAOProxy");
	        List temp_Students = Studentdao.getStudentById(id);
	       
	        for(int i=0;i<temp_Students.size();i++)
	        {
	        	
	        	Object[] obj = (Object[])temp_Students.get(i);
	        	Student s = new Student();
	        	s = (Student)obj[0];
	        	Special sp = new Special();
	        	sp = (Special)obj[1];
	        	Department d = new Department();
	        	d = (Department)obj[2];
	        	int begin=(currentpage-1)*pageresult;
	        	int end=currentpage*pageresult;
	        	if((begin<=i)&&(i<end))
	        	{
	        		objects.add(obj);
	        		students.add(s);
	        		specials.add(sp);
	        		departments.add(d);
	        	}
	        	obj = null;
	        	s = null;
	        	sp = null;
	        	d = null;
	        }
	        return SUCCESS;
	    }
	    
	    /**
	     * 根据分页信息得到非查询的数据
	     */
	    public String getAllStudents()
	    {
	    	//System.out.println(context.getRequest().getParameter("currentpage"));
	    	//System.out.println(context.getRequest().getParameter("pageresult"));
	    	ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
	        StudentDAO Studentdao = (StudentDAO)context.getBean("StudentDAOProxy");
	        List temp_Students = Studentdao.getAllStudentInfo();
	       System.out.println(temp_Students.size());
	       System.out.println("***********************************************************");
	       
	        for(int i=0;i<temp_Students.size();i++)
	        {
	        	
	        	Object[] obj = (Object[])temp_Students.get(i);
	        	Student s = new Student();
	        	s = (Student)obj[0];
	        	System.out.println("***********************************************************");
	        	System.out.println(s.getStuspecialId());
	        	System.out.println("***********************************************************");
	        	 
	        	Special sp = new Special();
	        	sp = (Special)obj[1];
	        	Department d = new Department();
	        	d = (Department)obj[2];
	        	
	        	
	        	System.out.println("***********************************************************");
	        	System.out.println(d.getDepartmentname());
	        	System.out.println("***********************************************************");
	        	int begin=(currentpage-1)*pageresult;
	        	int end=currentpage*pageresult;
	        	if((begin<=i)&&(i<end))
	        	{
	        		objects.add(obj);
	        		students.add(s);
	        		specials.add(sp);
	        		departments.add(d);
	        	}
	        	obj = null;
	        	s = null;
	        	sp = null;
	        	d = null;
	        }
	        //设置总页数
	        if(temp_Students.size()%pageresult==0)
	        	setPagecount(temp_Students.size()/pageresult);
	        else
	        	setPagecount(temp_Students.size()/pageresult+1);
	      //设置总记录数
	        setResultcount(temp_Students.size());
	        return SUCCESS;
	    }
	    
	    /**
	     * 根据分页信息得到查询的数据
	     */
	    public String searchStudents()
	    {
	    	setActiontype("2");//设置action类型为搜索
	    	setActionname("searchstudents");//设置action名称为搜索
	    	
	    	ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
	        StudentDAO Studentdao = (StudentDAO)context.getBean("StudentDAOProxy");
	        List temp_Students = Studentdao.searchStudents(stunumber,stuname,stusex,stuspecialId,specialdepartmentId);
	       
	        for(int i=0;i<temp_Students.size();i++)
	        {
	        	Object[] obj = (Object[])temp_Students.get(i);
	        	Student s = new Student();
	        	s = (Student)obj[0];
	        	Special sp = new Special();
	        	sp = (Special)obj[1];
	        	Department d = new Department();
	        	d = (Department)obj[2];
	        	int begin=(currentpage-1)*pageresult;
	        	int end=currentpage*pageresult;
	        	if((begin<=i)&&(i<end))
	        	{
	        		objects.add(obj);
	        		students.add(s);
	        		specials.add(sp);
	        		departments.add(d);
	        	}
	        	obj = null;
	        	s = null;
	        	sp = null;
	        	d = null;
	        }
	        //设置总页数
	        if(temp_Students.size()%pageresult==0)
	        	setPagecount(temp_Students.size()/pageresult);
	        else
	        	setPagecount(temp_Students.size()/pageresult+1);
	      //设置总记录数
	        setResultcount(temp_Students.size());
	        return SUCCESS;
	    }
	    
	    public File getUpload() {
	        return upload;
	    }

	    public void setUpload(File upload) {
	        this.upload = upload;
	    }

	    public String getUploadContentType() {
	        return uploadContentType;
	    }

	    public void setUploadContentType(String uploadContentType) {
	        this.uploadContentType = uploadContentType;
	    }

	    public String getUploadFileName() {
	        return uploadFileName;
	    }

	    public void setUploadFileName(String uploadFileName) {
	        this.uploadFileName = uploadFileName;
	    }

		/**
		 * @return the uploadDir
		 */
		public String getUploadDir() {
			return uploadDir;
		}

		/**
		 * @param uploadDir the uploadDir to set
		 */
		public void setUploadDir(String uploadDir) {
			this.uploadDir = uploadDir;
		}
		/**
		 * @return the currentpage
		 */
		public int getCurrentpage() {
			return currentpage;
		}

		/**
		 * @param currentpage the currentpage to set
		 */
		public void setCurrentpage(int currentpage) {
			this.currentpage = currentpage;
		}

		/**
		 * @return the pageresult
		 */
		public int getPageresult() {
			return pageresult;
		}

		/**
		 * @param pageresult the pageresult to set
		 */
		public void setPageresult(int pageresult) {
			this.pageresult = pageresult;
		}

		/**
		 * @return the stunumber
		 */
		public String getStunumber() {
			return stunumber;
		}

		/**
		 * @param stunumber the stunumber to set
		 */
		public void setStunumber(String stunumber) {
			this.stunumber = stunumber;
		}

		/**
		 * @return the stuname
		 */
		public String getStuname() {
			return stuname;
		}

		/**
		 * @param stuname the stuname to set
		 */
		public void setStuname(String stuname) {
			this.stuname = stuname;
		}

		/**
		 * @return the stusex
		 */
		public String getStusex() {
			return stusex;
		}

		/**
		 * @param stusex the stusex to set
		 */
		public void setStusex(String stusex) {
			this.stusex = stusex;
		}

		/**
		 * @return the students
		 */
		public List<Student> getStudents() {
			return students;
		}

		/**
		 * @param students the students to set
		 */
		public void setStudents(List<Student> students) {
			this.students = students;
		}

		/**
		 * @return the objects
		 */
		public List<Object> getObjects() {
			return objects;
		}

		/**
		 * @param objects the objects to set
		 */
		public void setObjects(List<Object> objects) {
			this.objects = objects;
		}

		/**
		 * @return the departments
		 */
		public List<Department> getDepartments() {
			return departments;
		}

		/**
		 * @param departments the departments to set
		 */
		public void setDepartments(List<Department> departments) {
			this.departments = departments;
		}

		/**
		 * @return the specials
		 */
		public List<Special> getSpecials() {
			return specials;
		}

		/**
		 * @param specials the specials to set
		 */
		public void setSpecials(List<Special> specials) {
			this.specials = specials;
		}

		/**
		 * @return the actiontype
		 */
		public String getActiontype() {
			return actiontype;
		}

		/**
		 * @param actiontype the actiontype to set
		 */
		public void setActiontype(String actiontype) {
			this.actiontype = actiontype;
		}

		/**
		 * @return the actionname
		 */
		public String getActionname() {
			return actionname;
		}

		/**
		 * @param actionname the actionname to set
		 */
		public void setActionname(String actionname) {
			this.actionname = actionname;
		}

		/**
		 * @return the pagecount
		 */
		public int getPagecount() {
			return pagecount;
		}

		/**
		 * @param pagecount the pagecount to set
		 */
		public void setPagecount(int pagecount) {
			this.pagecount = pagecount;
		}

		/**
		 * @return the resultcount
		 */
		public int getResultcount() {
			return resultcount;
		}

		/**
		 * @param resultcount the resultcount to set
		 */
		public void setResultcount(int resultcount) {
			this.resultcount = resultcount;
		}

		/**
		 * @return the totalspecials
		 */
		public List<Special> getTotalspecials() {
			return totalspecials;
		}

		/**
		 * @param totalspecials the totalspecials to set
		 */
		public void setTotalspecials(List<Special> totalspecials) {
			this.totalspecials = totalspecials;
		}

		/**
		 * @return the totaldepartments
		 */
		public List<Department> getTotaldepartments() {
			return totaldepartments;
		}

		/**
		 * @param totaldepartments the totaldepartments to set
		 */
		public void setTotaldepartments(List<Department> totaldepartments) {
			this.totaldepartments = totaldepartments;
		}

		/**
		 * @return the stuspecialId
		 */
		public String getStuspecialId() {
			return stuspecialId;
		}

		/**
		 * @param stuspecialId the stuspecialId to set
		 */
		public void setStuspecialId(String stuspecialId) {
			this.stuspecialId = stuspecialId;
		}

		/**
		 * @return the specialdepartmentId
		 */
		public String getSpecialdepartmentId() {
			return specialdepartmentId;
		}

		/**
		 * @param specialdepartmentId the specialdepartmentId to set
		 */
		public void setSpecialdepartmentId(String specialdepartmentId) {
			this.specialdepartmentId = specialdepartmentId;
		}

		/**
		 * @return the id
		 */
		public String getId() {
			return id;
		}

		/**
		 * @param id the id to set
		 */
		public void setId(String id) {
			this.id = id;
		}

	
		

}
